<<<<<<< HEAD
public class TestDemo {
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> map = new HashMap<>();
        for (String str : words) {
            if(map.get(str) == null){
                map.put(str,1);
            }else{
                int value = map.get(str);
                map.put(str,value+1);
            }
        }
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(k, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue() == o2.getValue()){
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue()-o2.getValue();
            }
        });
        for (Map.Entry<String,Integer> entry: map.entrySet()) {
            if(minHeap.size() < k){
                minHeap.offer(entry);
            }else{
                Map.Entry<String,Integer> flg = minHeap.peek();
                if(entry.getValue() > flg.getValue()){
                    minHeap.poll();
                    minHeap.offer(entry);
                }else if(entry.getValue() == flg.getValue()){
                    if(entry.getKey().compareTo(flg.getKey()) < 0 ){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        List<String> list = new ArrayList<>();
        while(!minHeap.isEmpty()){
            list.add(minHeap.poll().getKey());
        }
        Collections.reverse(list);
        return list;
    }
=======
public class TestDemo {
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> map = new HashMap<>();
        for (String str : words) {
            if(map.get(str) == null){
                map.put(str,1);
            }else{
                int value = map.get(str);
                map.put(str,value+1);
            }
        }
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(k, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue() == o2.getValue()){
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue()-o2.getValue();
            }
        });
        for (Map.Entry<String,Integer> entry: map.entrySet()) {
            if(minHeap.size() < k){
                minHeap.offer(entry);
            }else{
                Map.Entry<String,Integer> flg = minHeap.peek();
                if(entry.getValue() > flg.getValue()){
                    minHeap.poll();
                    minHeap.offer(entry);
                }else if(entry.getValue() == flg.getValue()){
                    if(entry.getKey().compareTo(flg.getKey()) < 0 ){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        List<String> list = new ArrayList<>();
        while(!minHeap.isEmpty()){
            list.add(minHeap.poll().getKey());
        }
        Collections.reverse(list);
        return list;
    }
>>>>>>> 83a8b80b6ef099ad7c56ca960b378bd45d44769e
}